package com.xuerhua.yxt.controller.sys.teacher;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.KccsCourseQueryVO;
import com.xuerhua.yxt.reqVo.TeacherReqVO;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.ExcelExportUtil;
import com.xuerhua.yxt.utils.ExportSetInfoUtil;
import com.xuerhua.yxt.view.TeacherInfoView;

@RequestMapping("/sys/teacher/manage")
@Controller
public class SysTeacherManageController extends BaseController {
	
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping(value={"","/"})
	public String Index() {
		return "sys/teacher/manage";
	}
	
	@RequestMapping("pageQuery")
	@ResponseBody Object pageQuery(TeacherReqVO vo){
		try {
			Page<TeacherInfo> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			page.setParams(vo);
			List<TeacherInfo> list = teacherService.selectFullListByPage(page);
			Grid grid = new Grid();
			grid.setRows(BeanUtils.copyAs(list, TeacherInfoView.class));
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("addUpdate")
	@ResponseBody Object addUpdate(TeacherInfo record) {
		try {
			if(record.getId() == null) {
				teacherService.insertSelective(record);
			}else {
				teacherService.updateById(record);
			}
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("queryById/{id}")
	@ResponseBody Object queryById(@PathVariable Integer id) {
		return teacherService.selectById(id);
	}
	
	@RequestMapping("export")
	ModelAndView workSignExport(TeacherReqVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			Page<TeacherInfo> page = new Page<>();
			page.setPageNo(vo.getPage());
			page.setPageSize(Integer.MAX_VALUE - 1);
			page.setParams(vo);
			List<TeacherInfo> list = teacherService.selectFullListByPage(page);
			//数据源
			LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
			String title="教师名单";
			map.put(title, BeanUtils.copyAs(list, TeacherInfoView.class));
			//标题  和  标题变量 
		    List<String[]> headNames = new ArrayList<String[]>();
		    headNames.add(new String[] { "姓名","年级号","任教学科","身份证件号","手机号","性别","生日","职称","籍贯","民族","政治面貌","居住地址","入党团时间","户口性质","婚姻状况","最高学历","毕业参加工作时间","大学专业","是否正编教师","是否有普通话证","是否具备心理ABC证","其他获得资格证书","曾获得荣誉"});
		    List<String[]> fieldNames = new ArrayList<String[]>();
		    fieldNames.add(new String[] { "name","gradeText","subjectText","idNo","mobile","sex","birthday","techTitle","nativePlace","nation","geopolitical","liveAddr","partyDate","homeNature","isMarry","education","workDate","schoolMajor","isOrganTeacher","isChineseCard","isPsychologyAbc","otherQualification","hadHonor"});
		    
		    //excel生成帮助类
		    List<Integer[]> columnFormat=new ArrayList<Integer[]>();
		    Integer [] format=new Integer[44];
		    columnFormat.add(format);
		    
		    String [] titles=new String[] {title};
		    //excel生成帮助类
		    ExportSetInfoUtil setInfo = new ExportSetInfoUtil(map,titles,headNames,fieldNames,columnFormat);
		    request.setAttribute("exportDatas", setInfo);
		    
		    //生成
			modelAndView=new ModelAndView(new ExcelExportUtil(title+".xls",""));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}
	
}
